package com.wd.mylibrary_login.activity

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.os.PersistableBundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory


class WeiXinActivity : AppCompatActivity(),IWXAPIEventHandler {
// APP_ID 替换为你的应用从官方网站申请到的合法appID
    private val APP_ID = "wx064904aa8809f5aa"

    // IWXAPI 是第三方app和微信通信的openApi接口
    private lateinit var api: IWXAPI

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, APP_ID, true)

        // 将应用的appId注册到微信
        api.registerApp(APP_ID)

        api.handleIntent(getIntent(),this)
    }

    override fun onReq(p0: BaseReq?) {

    }

    override fun onResp(p0: BaseResp?) {

        Toast.makeText(this, "微信登录成功了", Toast.LENGTH_SHORT).show()
        startActivity(Intent(this, StartActivity::class.java))
        // 结束当前activity
        finish()
    }

}

